﻿namespace OnHook.Framework.Ioc
{
    public class NamedServiceProvider(IServiceProvider serviceProvider, IDictionary<string, Type> registrations) : INamedServiceProvider
    {
        private readonly IServiceProvider _serviceProvider = serviceProvider;
        private readonly IDictionary<string, Type> _registrations = registrations;

        public TService? GetService<TService>(string serviceName)
        {
            string mapServiceName = $"{typeof(TService).FullName}_{serviceName}";
            if (!_registrations.TryGetValue(mapServiceName, out var implementationType))
                throw new ArgumentException($"Service \"{serviceName}\" is not registered in container");
            var typeList = _serviceProvider.GetService(typeof(IEnumerable<TService>)) as IEnumerable<TService> ?? new List<TService>();
            return typeList.FirstOrDefault(t =>t?.GetType().FullName == implementationType.FullName);
            //return (TService)_serviceProvider.GetService(implementationType)!;
        }
    }
}
